package com.czn.shopdemo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czn.shopdemo.model.Business;
import com.czn.shopdemo.model.Goods;
import com.czn.shopdemo.model.Result;
import com.czn.shopdemo.model.User;
import com.czn.shopdemo.model.vo.GoodsVO;
import com.czn.shopdemo.model.vo.LoginVO;
import com.czn.shopdemo.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author Zenan Chen
 * @date 2023/4/4 9:26
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
public class UserController {

    private static final Logger log = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @PostMapping("/save")
    @ApiOperation("保存用户")
    public Result login(@RequestBody User user) {
        try {
            log.info("接受到参数：user:{}", JSON.toJSONString(user));
            log.info("------------------------->/save 保存用户开始");
            userService.saveUser(user);
            log.info("------------------------->/save 保存用户结束");
            return Result.ok("保存用户成功！");
        } catch (Exception e) {
            log.error("------------------------->/save 保存用户异常【{}】", e.getMessage());
            return Result.fail("保存用户异常：" + e.getMessage());
        }
    }

    @ApiOperation("删除用户")
    @PostMapping("/deleteUser")
    public Result deleteUser(@RequestParam String id) {
        try {
            log.info("接受到参数：id:{}", id);
            log.info("------------------------->/deleteUser 删除用户开始");
            userService.deleteUser(id);
            log.info("------------------------->/deleteUser 删除用户结束");
            return Result.ok("删除用户成功！");
        } catch (Exception e) {
            log.error("------------------------->/deleteUser 删除用户异常【{}】", e.getMessage());
            return Result.build(201, e.getMessage());
        }
    }

    @PostMapping("/login")
    @ApiOperation("用户登录")
    public Result login(@RequestBody LoginVO loginVo) {
        try {
            log.info("接受到参数：loginVo:{}", JSON.toJSONString(loginVo));
            log.info("------------------------->/api/user/login 用户登录开始");
            Map<String, String> result = userService.login(loginVo);
            log.info("------------------------->/api/user/login 用户登录结束");
            return Result.ok(result);
        } catch (Exception e) {
            log.error("------------------------->/api/user/login 用户登录异常【{}】", e.getMessage());
            return Result.fail("用户登录异常：" + e.getMessage());
        }
    }

    @ApiOperation("获取用户详情")
    @PostMapping("/getUserDetail")
    public Result getUserDetail(@RequestParam String id) {
        try {
            log.info("接受到参数：id:{}", id);
            log.info("------------------------->/getUserDetail 获取用户详情开始");
            User user = userService.getUserDetail(id);
            log.info("------------------------->/getUserDetail 获取用户详情结束");
            return Result.ok(user);
        } catch (Exception e) {
            log.error("------------------------->/getUserDetail -获取用户详情异常【{}】", e.getMessage());
            return Result.build(201, e.getMessage());
        }
    }

    @ApiOperation("查询用户列表")
    @PostMapping("/getUserList")
    public Result getUserList(@RequestParam Integer pageNo,
                              @RequestParam Integer pageSize,
                              @RequestParam(required = false) String keyword) {
        try {
            log.info("接受到参数：page:{},limit:{},keyword:{}", pageNo, pageSize, keyword);
            log.info("------------------------->/getUserList 查询用户列表开始");
            Page<User> result = userService.getUserList(pageNo, pageSize, keyword);
            log.info("------------------------->/getUserList 查询用户列表结束");
            return Result.ok(result);
        } catch (Exception e) {
            log.error("------------------------->/getUserList 查询用户列表异常【{}】", e.getMessage());
            return Result.build(201, e.getMessage());
        }
    }
}
